package autoteam;

import java.util.ArrayList;
import java.util.List;
import db.*;

public class SearchEngine
{
	// search players or coaches by their first/last names
	public static List<DBRecord> searchPeople(String table, String query)
	{
		DataAccessObject dao = new DataAccessObject();
		String[] token = query.split(" ");
		List<DBRecord> recordList = dao.getAllRecords(table);
		List<DBRecord> toReturn = new ArrayList<DBRecord>();
		
		/* when no query is entered, display all records
		   remove comments if no record should be displayed */
		// if (query.equals(""))
			// return toReturn;
		
		// search
		for (DBRecord r : recordList)
		{
			for (int i = 0; i < token.length; i++)
			{
				String str1 = ((String) r.getFieldValue("lastname")).toLowerCase();
				String str2 = ((String) r.getFieldValue("firstname")).toLowerCase();
				String str3 = token[i].toLowerCase();
				
				// if query is a substring
				int j = (str1.indexOf(str3));
				int k = (str2.indexOf(str3));
				if (j != -1 || k != -1)
				{
					toReturn.add(r);
					break;
				}
				
				// some crazy algorithm i'll have no idea about
				// after 2 days
				j = -1;
				k = -1;
				int n1 = str1.length();
				int n2 = str2.length();
				int n3 = str3.length();
				
				if (n1 >= n3 - 1 && n1 <= n3 + 1)
				{
					int m = Math.min(n1, n3) / 2 + 1;
					String begin1 = str1.substring(0, m);
					String end1 = str1.substring(n1 - m, n1);
					String begin3 = str3.substring(0, m);
					String end3 = str3.substring(n3 - m, n3);
					if ((begin1.equals(begin3) || end1.equals(end3)) && m > 2)
						j = 0;
				}
				
				if (n2 >= n3 - 1 && n2 <= n3 + 1)
				{
					int m = Math.min(n2, n3) / 2 + 1;
					String begin2 = str2.substring(0, m);
					String end2 = str2.substring(n2 - m, n2);
					String begin3 = str3.substring(0, m);
					String end3 = str3.substring(n3 - m, n3);
					if ((begin2.equals(begin3) || end2.equals(end3)) && m > 2)
						k = 0;
				}
				
				if (j != -1 || k != -1)
				{
					toReturn.add(r);
					break;
				}
			}
		}
		
		return toReturn;
	}
	
	// search team by name
	public static List<DBRecord> searchTeam(String query)
	{
		DataAccessObject dao = new DataAccessObject();
		String[] token = query.split(" ");
		List<DBRecord> recordList = dao.getAllRecords("teams");
		List<DBRecord> toReturn = new ArrayList<DBRecord>();
		for (DBRecord r : recordList)
		{
			for (int i = 0; i < token.length; i++)
			{
				String str1 = ((String) r.getFieldValue("teamname")).toLowerCase();
				String str2 = token[i].toLowerCase();
				int j = (str1.indexOf(str2));
				if (j != -1)
				{
					toReturn.add(r);
					break;
				}
			}
		}
		return toReturn;
	}
	
	// search league by name
	public static List<DBRecord> searchLeague(String query)
	{
		DataAccessObject dao = new DataAccessObject();
		String[] token = query.split(" ");
		List<DBRecord> recordList = dao.getAllRecords("leagues");
		List<DBRecord> toReturn = new ArrayList<DBRecord>();
		for (DBRecord r : recordList)
		{
			for (int i = 0; i < token.length; i++)
			{
				String str1 = ((String) r.getFieldValue("leaguename")).toLowerCase();
				String str2 = token[i].toLowerCase();
				int j = (str1.indexOf(str2));
				if (j != -1)
				{
					toReturn.add(r);
					break;
				}
			}
		}
		return toReturn;
	}
	
	// search all tables
	public static List<DBRecord> searchAll(String query)
	{
		List<DBRecord> recordList = searchPeople("players", query);
		recordList.addAll(searchPeople("coaches", query));
		recordList.addAll(searchTeam(query));
		recordList.addAll(searchLeague(query));
		return recordList;
	}
	
	public static void main(String[] args)
	{
		List<DBRecord> recordList = SearchEngine.searchAll("karan");
		for (DBRecord r : recordList)
			System.out.println(r.getFieldValue("firstname") +
					" " + r.getFieldValue("lastname"));
	}
}
